﻿/// <reference path="~/Scripts/Library/jquery-1.9.1.min.js" />

var isWindowResizing = false;

function Window_OnResize() {

    if (isWindowResizing) { return; }

    isWindowResizing = true;



    var availableHeight = $(window).innerHeight();

    availableHeight -= $("#HomeContent").offset().top * 2; // REMOVE MARGIN HEIGHT

    availableHeight += 6; // ADD BACK BORDER HEIGHT

    if (availableHeight < 400) { availableHeight = 400; } // MAKE SURE MINIMUM SIZE

    $("#HomeContent").height(availableHeight);

    $("td.Content").height(availableHeight);

    availableHeight = availableHeight - $("#SearchResultsGridContainer").position().top + ($("#SearchResultsGridPager").height() * 2);


    $("#SearchResultsGrid").setGridWidth(1); // SHRINK GRID SO THAT THE COLUMN CAN BE SIZED 

    $("#SearchResultsGrid").setGridHeight(1); // SHRINK GRID SO THAT THE COLUMN CAN BE SIZED 

    // RESIZE CONTENT COLUMNS TO REFIT GRID

    if (availableHeight < 100) { availableHeight = 100; }

    $("#SearchResultsGridContainer").height(availableHeight);


    // RESIZE GRID ON SEPARATE THREAD (TO ALLOW REDRAW TO FINISH)

    setTimeout(function () { jqGrid_ResizeToFit("SearchResultsGrid"); }, 1);


    isWindowResizing = false;

}

function SearchResultsGrid_Formatter_Action(cellValue, options, rowObject) {

    var formattedCellValue = "";

    var iconEdit = "<span class=\"ui-icon ui-icon-newwin\"></span>";

    // USE THE SELECTED OBJECT TYPE VERSUS THE INCOMING OBJECT TYPE

    // THIS IS TO SUPPORT WHEN ONE OBJECT TYPE IS USED BY SEPARATE CONFIG OPTIONS (E.G. ROLE COULD BE ENTERPRISE OR ENVIRONMENT)

    var href = cellValue;

    var target = rowObject.ObjectType + "_" + rowObject.Id;

    var clickEvent = "window.open ('" + href + "', '" + target + "', 'directories=no,location=no,menubar=no,resizable=yes,status=yes,titlebar=no,toolbar=no'); false;";

    formattedCellValue += "<div class=\"ui-widget ui-state-default ui-corner-all\" style=\"display: inline-block; float: left; width: 16px;\"><a href=\"javascript:void(0);\" onclick=\"" + clickEvent + "\">" + iconEdit + "</a></div>";

    return formattedCellValue;

}

function SearchResultsGrid_Initialize(requestUrl) {

    $("#SearchResultsGrid").jqGrid({

        width: 1,

        url: requestUrl,

        datatype: "json",

        rowNum: 50,

        rowList: [10, 25, 50],

        colModel: [

            { name: "ObjectType", label: "Type" },

            { name: "Name", label: "Name" },

            { name: "Detail1", label: "Detail1" },

            { name: "Detail2", label: "Detail2" },

            { name: "Detail3", label: "Detail3" },

            { name: "ActionLink", label: " ", width: 20, fixed: true, search: false, sortable: false, align: "center", formatter: SearchResultsGrid_Formatter_Action }

        ],


        jsonReader: { repeatitems: false },

        pager: "#SearchResultsGridPager",

        loadComplete: function (data) { SearchResultsGrid_OnLoadComplete(data); },

        loadError: function (xhr, status, error) { SearchResultsGrid_OnLoadError(xhr, status, error); }

    });

    jqGrid_RemoveCorners("SearchResultsGrid");


    $("#SearchResultsGrid").jqGrid("navGrid", "#SearchResultsGridPager", { edit: false, add: true, del: false, search: false });

    $("#SearchResultsGridPager_left").removeAttr("style");


    // ADD FILTERTOOBAR 

    $("#SearchResultsGrid").jqGrid("filterToolbar", { searchOnEnter: true, stringResult: true });

    return;

}

function SearchResultsGrid_OnLoadComplete(data) {

    return;

}

function SearchResultsGrid_OnLoadError(xhr, status, error) {

    alert("Error Loading Data from the Server: " + error);

    return;

}

function SearchResultsGrid_Refresh() {

    if ($("#SearchResultsGridPager_left").length > 0) {

        $("#SearchResultsGrid").jqGrid().GridUnload();

    }

    var searchCriteria = escape($("#SearchCriteria").val());

    var searchUrl = $("#RootUrl").val() + "Home/GlobalSearch?Criteria=" + searchCriteria + "&limitTo=";

    SearchResultsGrid_Initialize(searchUrl);

    Window_OnResize();

}

function GlobalSearch_OnClick() {
    
    SearchResultsGrid_Refresh();

}


$(document).ready(function () {

    $("#ApplicationTitleBarLinkHome").hide();


    $(window).resize(Window_OnResize);

    $(window).resize();


    $("#SearchCriteria").focus();

});
